#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<graphics.h>
#pragma comment(lib,"Winmm.lib")

//全局变量
int bird_x,bird_y;//小鸟的坐标 
int high,width;//游戏画面尺寸 
int bar_y,bar_xtop,bar_xdown;//障碍物坐标 
int score;//
IMAGE img_bk,img_bd1,img_bd2,img_bar_up1,img_bar_up2,img_bar_down1,img_bar_down2;



void gotoxy(int x,int y)//光标移动到（x，y） 位置 
{
	HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition (handle, pos);
} 





void hide()//隐藏光标 
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
} 




void starup()//游戏初始化 
{	
	


	mciSendString("open D:\\background.mp3 alias bkmusic",NULL,0,NULL);//背景音乐
	mciSendString("play bkmusic repeat",NULL,0,NULL);//循环播放
	initgraph(350,600);
	loadimage(&img_bk,"D:\\background.jpg");
	loadimage(&img_bd1,"D:\\bird1.jpg");
	loadimage(&img_bd2,"D:\\bird2.jpg");
	loadimage(&img_bar_up1,"D:\\bar_up1.gif");
	loadimage(&img_bar_up2,"D:\\bar_up2.gif");
	loadimage(&img_bar_down1,":\\bar_down1.gif");
	loadimage(&img_bar_down2,"D:\\bar_down2.gif");
	bird_x=50;
	bird_y=350;
	BeginBatchDraw();
	bar_y=150;
	bar_xtop=-300;
	bar_xdown=400;

	hide() ;
}





void show()//游戏显示
{	
	putimage(0,0,&img_bk);	//显示背景
	putimage(bar_y,bar_xtop,&img_bar_up1,NOTSRCERASE);	//显示上一半遮挡物
	putimage(bar_y,bar_xtop,&img_bar_up2,SRCINVERT);
	putimage(bar_y,bar_xdown,&img_bar_down1,NOTSRCERASE);
	putimage(bar_y,bar_xdown,&img_bar_down2,SRCINVERT);
	putimage(bird_x,bird_y,&img_bd1,NOTSRCERASE);
	putimage(bird_x,bird_y,&img_bd2,SRCINVERT);




	FlushBatchDraw();
	Sleep(50);
	

  
}




void updataWithoutInput()//用户无关设置
{	


	if(bird_y<580)
		bird_y=bird_y+3;
		bar_y=bar_y-3;
	if(bar_y<=-140)
	{
		bar_y=350;
		int randposition=rand()%100;
		bar_xtop=-5*randposition;
		bar_xdown=bar_xtop+700;
	}
	
	
	else if(bird_y>bar_xdown||bird_y<bar_xtop||bird_y>=600||bird_y<=0)
	{
		printf("You are died!");
		exit(0); 
		
	}
	






}	




void updataWithInput()//用户有关设置
{
	char input;
	if(kbhit())			//判断输入是否有误
	{
		input=getch();
		if(input==' '&&bird_y>20)
		{
			bird_y=bird_y-60;
			mciSendString("close jpmusic",NULL,0,NULL); //先把前一次音乐关掉
			mciSendString("open D:\\Jump.mp3 alias  jpmusic",NULL,0,NULL);//打开音效
			mciSendString("play jpmusic",NULL,0,NULL);			//仅播放一次





		}
	} 
}



void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
	




}

int main()
{
	hide(); 
	starup();//数据初始化
	while(1)//游戏循环 
	{
		show();//游戏显示 
		updataWithoutInput();//用户无关设置 
		updataWithInput(); //用户有关设置 
	} 
	gameover();
	return 0; 
} 